<?php
namespace app\common\model;

use app\common\model\ManagerRole as ManagerRoleModel;

class ChannelAccount extends Base
{
    protected $table = 'channel_accounts';

    protected static function init()
    {

    }

    public function getCreateTimeLabelAttr($value, $data)
    {
        if (empty($data['create_time'])) {

            return '';
        }

        return date('Y-m-d H:i:s', $data['create_time']);
    }

    public function getUpdateTimeLabelAttr($value, $data)
    {
        if (empty($data['update_time'])) {

            return '';
        }

        return date('Y-m-d H:i:s', $data['update_time']);
    }

    public function getLastLoginIpLabelAttr($value, $data)
    {
        if (!empty($data['last_login_ip'])) {
            return long2ip($data['last_login_ip']);
        }

        return '';
    }

    public function getLastLoginTimeLabelAttr($value, $data)
    {
        if (!empty($data['last_login_time'])) {
            return date('Y-m-d H:i:s', $data['last_login_time']);
        }

        return '';
    }

    public function getShareUrlAttr($value, $data)
    {
        if (is_https()) {
            $http = 'https';
        } else {
            $http = 'http';
        }
        $channel = Channel::get(array('id' => $data['channel_id']));

        $share_url = $http . '://' . $_SERVER['HTTP_HOST'] . '/channel.html?code=' . $channel->channel_code . '&id=' . $data['id'];

        return $share_url;
    }

    public function getRoleIdsAttr($value, $data)
    {
        $temp = ManagerRoleModel::where('manager_id','=', $data['id'])->where('module_name', '=', 'channel')->column('role_id');

        return $temp;
    }

    public function getChannelNameAttr($value, $data)
    {
        $channel = Channel::get(array('id' => $data['channel_id']));
        if (empty($channel)) {
            return '';
        }

        return $channel['name'];
    }
}